var express = require("express");
var router = express.Router();
const fs = require("fs");

const { getTimeWithStamp } = require("../utils/date");
const { getNextSequenceValue, orderFind } = require("./funcs");
const { getIOInstance } = require("../utils/socket");
const { randomString } = require("../utils/index");
const Order = require("../mongoose/models/order");

/* GET home page. */
/**
 * 订单列表
 */
router.get("/list", async (req, res, next) => {
  orderFind((orders) => {
    if (orders) {
      res.status(200).send({
        code: 1,
        data: orders,
        msg: "操作成功",
      });
    } else {
      res.status(500).send({
        code: 999,
        msg: orders,
      });
    }
  }, req.query);
});

/**
 * 创建订单
 */
router.post("/createOrder", async (req, res, next) => {
  const { foods, price, name, openid, store, remark } = req.body;

  let order_time = getTimeWithStamp(Date.now());
  let newno = String(new Date().getTime()).substring(0, 8);
  let obj = {
    id: await getNextSequenceValue("order_id"),
    name,
    foods: JSON.parse(foods),
    order_time,
    price,
    openid,
    order_no: newno + randomString(10),
    store,
    remark
  };

  console.log(obj, "obj");

  new Order(obj).save(function (err, order) {
    if (err) {
      res.send({
        code: 999,
        msg: err,
      });
    } else if (!order) {
      res.send({
        code: 999,
        msg: "网络错误",
      });
    } else {
      console.log("test");
      try {
        getIOInstance().emit("createOrderSuccess", { order_id: obj.id });
      } catch (err) {
        res.send({
          code: 500,
          msg: err,
        });
      }

      res.send({
        code: 1,
        data: order,
        msg: "添加成功!",
      });
    }
  });
});

router.get("/detail", (req, res) => {
  const { order_no } = req.query;
  console.log(order_no, "order_no");
  Order.findOne({ order_no }, (err, data) => {
    if (err) {
      res.send({
        code: 500,
        msg: err,
      });
      return;
    }
    res.send({
      code: 1,
      msg: "查询成功",
      data,
    });
  });
});

router.get("/insertoneprop", async function (req, res, next) {
  if (req.query.key !== "INSERTONEPROPREWCSQWEJGKR123DFG12") {
    res.send({
      code: 500,
      msg: "暂无权限",
    });
    return;
  }
  Order.updateMany({}, { store: "1" }, function (err, docs) {
    if (err) {
      res.send({
        code: 500,
        msg: err,
      });
    } else {
      res.send({
        code: 1,
        msg: "操作成功！",
      });
    }
  });
});

router.post('/updateOrderStatus', (req, res) => {
  if (req.body.status != '1') {
    res.send({
      code: 500,
      msg: "该用户还没有支付!!",
    });
    return
  }
  Order.findOneAndUpdate({ id: req.body.id }, { order_status: '已完成' }, null, (err, data) => {
    if (err) {
      console.log(err, 'err 没有找到订单~');
      res.send({
        code: 500,
        msg: "网络错误 " + err,
      });
      return;
    }
    res.send({
      code: 1,
      msg: "修改成功",
    });
  });
})
module.exports = router;
